/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.dbdir.connections;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import org.dbdir.core.Logger;

/**
 *
 * @author thotheolh
 */
public class TCPServerSocket{

    private Socket socket = null;
    private ServerSocket servsocks = null;
    private int port;
    private Logger logger;

    public TCPServerSocket(int port, Logger logger) {
        this.setPort(port);
        this.logger = logger;
    }

    public void getSocksConnection() {
        try {
            if (getSocket() == null && getServsocks() == null) {
                setServsocks(new ServerSocket(getPort()));
                setSocket(getServsocks().accept());
            }

        } catch (IOException ex) {
            logger.log(3, ex.getMessage());
        }
    }

    public void resetConnection() {
        setSocket(null);
        setServsocks(null);
        getSocksConnection();
    }

    public void killConnection() {
        if (getSocket() != null && getServsocks() != null) {
            try {
                getServsocks().close();
                getSocket().close();
            } catch (IOException ex) {
                logger.log(3, ex.getMessage());
            }

        }
        setSocket(null);
        setServsocks(null);
    }

    /**
     * @return the socket
     */
    public Socket getSocket() {
        return socket;
    }

    /**
     * @param socket the socket to set
     */
    public void setSocket(Socket socket) {
        this.socket = socket;
    }

    /**
     * @return the servsocks
     */
    public ServerSocket getServsocks() {
        return servsocks;
    }

    /**
     * @param servsocks the servsocks to set
     */
    public void setServsocks(ServerSocket servsocks) {
        this.servsocks = servsocks;
    }

    /**
     * @return the port
     */
    public int getPort() {
        return port;
    }

    /**
     * @param port the port to set
     */
    public void setPort(int port) {
        this.port = port;
    }
}
